/**
 * 
 */
package com.jjt.kiki.util;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * @author jiangjintai
 *
 */
public class MyHttpClient {
	Map<String, String> headerMap = null;

	public String sendJsonRequest(String url, String json,
			Map<String, String> headers) throws ClientProtocolException,
			IOException {
		this.headerMap = headers;
		return this.sendJsonRequest(url, json);
	}

	public String sendJsonRequest(String url, String json)
			throws ClientProtocolException, IOException {
		HttpClient httpclient = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		if (this.headerMap != null) {
			this.setHeader(post);
		}
		StringEntity s = new StringEntity(json.toString());
		post.setEntity(s);
		HttpResponse response = httpclient.execute(post);
		return paseResponse(response);

	}

	public String post(String url, Map<String, String> params)
			throws ClientProtocolException, IOException {
		HttpClient httpclient = HttpClients.createDefault();
		String body = null;

		System.out.println("create httppost:" + url);
		HttpPost post = postForm(url, params);
		HttpResponse respoonse = httpclient.execute(post);
		body = paseResponse(respoonse);
		return body;
	}

	public String post(String url, Map<String, String> params,
			Map<String, String> headerMap) throws ClientProtocolException,
			IOException {
		this.headerMap = headerMap;
		return this.post(url, params);
	}

	public String get(String url, Map<String, String> params,
			Map<String, String> header) throws ClientProtocolException,
			IOException {
		this.headerMap = header;
		return this.get(url, params);
	}

	public String get(String url, Map<String, String> params)
			throws ClientProtocolException, IOException {
		HttpClient httpclient = HttpClients.createDefault();

		if (params != null) {
			StringBuffer sb = new StringBuffer();
			sb.append(url);
			sb.append("?");
			for (Entry<String, String> entry : params.entrySet()) {
				StringBuffer sbParam = new StringBuffer();
				sbParam.append(entry.getKey());
				sbParam.append("=");
				sbParam.append(entry.getValue());
				sbParam.append("&");
				sb.append(sbParam);
			}
			sb.replace(sb.length() - 1, sb.length(), "");
			System.out.println("add Param then " + sb.toString());
			url = sb.toString();
		}
		System.out.println("create httppost:" + url);
		HttpGet get = new HttpGet(url);
		if (this.headerMap != null)
			this.setHeader(get);
		HttpResponse response = httpclient.execute(get);

		return paseResponse(response);
	}

	/**
	 * jiangjintai 2016年8月23日
	 * 
	 * @param get
	 */
	private void setHeader(HttpGet get) {
		if (this.headerMap != null) {
			Set<String> keySet = headerMap.keySet();
			for (String key : keySet) {
				get.setHeader(key, headerMap.get(key));
			}
		}

	}

	private String paseResponse(HttpResponse response) {
		System.out.println("get response from http server..");
		HttpEntity entity = response.getEntity();

		System.out.println("response status: " + response.getStatusLine());
		String body = null;
		try {
			body = EntityUtils.toString(entity);
			System.out.println(body);
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}

	private HttpPost postForm(String url, Map<String, String> params) {

		HttpPost httpost = new HttpPost(url);
		setHeader(httpost);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();

		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			nvps.add(new BasicNameValuePair(key, params.get(key)));
		}

		System.out.println("set utf-8 form entity to httppost");
		httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

		return httpost;
	}

	/**
	 * jiangjintai 2016年8月22日
	 * 
	 * @param httpost
	 */
	private void setHeader(HttpPost httpPost) {
		if (this.headerMap != null) {
			Set<String> keySet = headerMap.keySet();
			for (String key : keySet) {
				httpPost.setHeader(key, headerMap.get(key));
			}
		}

	}

	public String sendFile(String url, String filePath)
			throws ClientProtocolException, IOException {

		HttpClient httpclient = HttpClients.createDefault();
		HttpPut httpput = new HttpPut(url);
		if(this.headerMap!=null){
			this.setHeader(httpput);
		}
		FileEntity fileEntity = new FileEntity(new File(filePath));
		httpput.setEntity(fileEntity);
		HttpResponse response = httpclient.execute(httpput);
		return this.paseResponse(response);
	}
	
	/**
	 * jiangjintai
	 * 2016年8月23日
	 * @param httpput
	 */
	private void setHeader(HttpPut httpput) {
		if (this.headerMap != null) {
			Set<String> keySet = headerMap.keySet();
			for (String key : keySet) {
				httpput.setHeader(key, headerMap.get(key));
			}
		}
		
	}

	public String sendFile(String url,String filePath,Map<String,String> headers) throws ClientProtocolException, IOException{
		this.headerMap=headers;
		return this.sendFile(url, filePath);
	}

}
